--- title: "L2-052 吉利矩阵" created: 2025-11-28 tags: - 算法 --- # L2-052 吉利矩阵 ## 题目 [L2-052 吉利矩阵](https://pintia.cn/problem-sets/994805046380707840/exam/problems/type/7?problemSetProblemId=1781658570803388427&page=1) ![[image-c1c00b7e.png]] ## 思路分析 类似于n皇后问题的 从点入手 看每个位置能放什么 ## 代码实现 最淳朴的方式: 4/25 ```cpp #include using namespace std; #define endl '\n' using ll = long long; using ull = unsigned long long; using PII = pair; using Pll = pair; int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1}; const int inf = 0x3f3f3f3f; int l,n; int g[5][5]; int ans=0; bool check(){ for(int i=0;i>l>>n; dfs(0,0); cout< using namespace std; #define endl '\n' using ll = long long; using ull = unsigned long long; using PII = pair; using Pll = pair; int dx[4]= {-1,0,1,0},dy[4]= {0,1,0,-1}; const int inf = 0x3f3f3f3f; int l,n; int g[5][5]; ll ans=0; void dfs(int x,int y,vector& sum_row,vector& sum_col){ if(x==n){ for(int i=0;il || sum_col[y]+i>l) break; int nx=x,ny=y+1; if(ny==n) nx++,ny=0; g[x][y]=i; sum_row[x]+=i; sum_col[y]+=i; dfs(nx,ny,sum_row,sum_col); sum_row[x]-=i; sum_col[y]-=i; } } int main() { ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); cin>>l>>n; vector sum_row(n,0),sum_col(n,0); dfs(0,0,sum_row,sum_col); cout<